package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.zhgd.annotation.ZhjdDataFilter;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NMaterielProjectBo;
import com.ruoyi.zhgd.domain.vo.NMaterielProjectVo;
import com.ruoyi.zhgd.domain.NMaterielProject;
import com.ruoyi.zhgd.mapper.NMaterielProjectMapper;
import com.ruoyi.zhgd.service.INMaterielProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目信息Service业务层处理
 *
 * @author cj
 * @date 2023-10-09
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NMaterielProjectServiceImpl implements INMaterielProjectService {

    private final NMaterielProjectMapper baseMapper;

    /**
     * 查询项目信息
     */
    @Override
    public NMaterielProjectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目信息列表
     */
    @Override
    public TableDataInfo<NMaterielProjectVo> queryPageList(NMaterielProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NMaterielProject> lqw = buildQueryWrapper(bo);
        Page<NMaterielProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目信息列表
     */
    @Override
    public List<NMaterielProjectVo> queryList(NMaterielProjectBo bo) {
        LambdaQueryWrapper<NMaterielProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NMaterielProject> buildQueryWrapper(NMaterielProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NMaterielProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NMaterielProject::getBaseCode, bo.getBaseCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NMaterielProject::getName, bo.getName());
        lqw.eq(bo.getMoney() != null, NMaterielProject::getMoney, bo.getMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getChargePerson()), NMaterielProject::getChargePerson, bo.getChargePerson());
        lqw.eq(StringUtils.isNotBlank(bo.getTellphone()), NMaterielProject::getTellphone, bo.getTellphone());
        lqw.eq(bo.getEnable() != null, NMaterielProject::getEnable, bo.getEnable());
        lqw.eq(bo.getCreator() != null, NMaterielProject::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NMaterielProject::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NMaterielProject::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NMaterielProject::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增项目信息
     */
    @Override
    public Boolean insertByBo(NMaterielProjectBo bo) {
        NMaterielProject add = BeanUtil.toBean(bo, NMaterielProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目信息
     */
    @Override
    public Boolean updateByBo(NMaterielProjectBo bo) {
        NMaterielProject update = BeanUtil.toBean(bo, NMaterielProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NMaterielProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @ZhjdDataFilter
    public List<NMaterielProject> selectList(Map<String, Object> params) {
        QueryWrapper<NMaterielProject> queryWrapper = new QueryWrapper<NMaterielProject>();
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("base_code, name");
        return baseMapper.selectList(queryWrapper);
    }
}
